<template>
  <div class="orderBox">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row>
        <el-col :span="8">
          <div class="grid-content bg-purple title" style="text-align: left">
            <el-input
              clearable
              placeholder="请输入内容"
              class="input-with-select"
              @keydown.native.enter="getSearchOrders"
              @clear="getSearchOrders"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getSearchOrders"
              ></el-button>
            </el-input>
          </div>
        </el-col>
      </el-row>

      <el-table v-Loading = "isLoading" :data="orderList" border style="width: 100%">
        <el-table-column type="index" label="#" width="50"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号" min-width="250">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column prop="pay_status" label="是否付款">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              v-if="scope.row.pay_status == '1'"
              >已付款</el-button
            >
            <el-button size="mini" type="danger" v-else>未付款</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货">
          <!-- <template slot-scope="scope">
            <span v-if="scope.row.is_send == '1'">是</span>
            <span v-else>否</span>
          </template> -->
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            <span>{{ scope.row.create_time | filterDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="修改订单地址"
              placement="top"
            >
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="handleEditAddress(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="物流信息"
              placement="top"
            >
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-location"
                @click="handleEditHostMan(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="orderParams.pagenum"
        :page-sizes="[5, 10, 15, 25]"
        :page-size="orderParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalSize"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址 -->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisibleAddress">
      <el-form :model="addressForm" :rules="rules">
        <el-form-item
          label="省市区县"
          prop="address1"
          :label-width="formLabelWidth"
        >
          <!-- <el-input v-model="addressForm.name" autocomplete="off"></el-input> -->
          <el-cascader
            v-model="addressForm.address1"
            :options="cityData"
            :props="{ expandTrigger: 'hover' }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="" :label-width="formLabelWidth">
          <el-input
            v-model="addressForm.address2"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDecidedAddress">取 消</el-button>
        <el-button type="primary" @click="decidedAddress">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 物流信息 -->
    <el-dialog title="物流进度" :visible.sync="dialogFormVisiblePostMan">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in postManList"
          :key="index"
          :icon="activity.icon"
          :type="activity.type"
          :color="activity.color"
          :size="activity.size"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisiblePostMan = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisiblePostMan = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getOrders, getPostMan } from "../../api/index";
import cityData from "./citydata.js";

export default {
  data() {
    return {
      isLoading: false,
      orderList: [],
      orderParams: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      totalSize: 0, //总条数,
      dialogFormVisibleAddress: false,
      dialogFormVisiblePostMan: false,
      formLabelWidth: "120px",
      rules: {
        address1: [
          { required: true, message: "请输入省市区", trigger: "blur" },
        ],
        address2: [],
      },
      addressForm: {
        address1: "",
        address2: "",
      },
      cityData,
      postManList: [],
    };
  },
  created() {
    this.initOrdersList();
  },
  methods: {
    async initOrdersList() {
      let {
        data: { goods, total },
        meta: { status, msg },
      } = await getOrders(this.orderParams);
      // console.log(data)
      if (status != 200) return this.$message.error(msg);
      this.isLoading = false
      this.orderList = goods;
      this.totalSize = total;
    },
    // 分页功能
    handleSizeChange(val) {
      this.orderParams.pagesize = val;
      this.initOrdersList();
    },
    handleCurrentChange(val) {
      this.orderParams.pagenum = val;
      this.initOrdersList();
    },
    //搜索功能
    getSearchOrders() {},

    handleEditAddress() {
      this.dialogFormVisibleAddress = true;
    },

    // 三级联动
    handleChange(value) {
      console.log(value);
    },
    // 确认
    decidedAddress() {},
    cancelDecidedAddress() {
      this.dialogFormVisibleAddress = false;
      this.addressForm = {};
    },

    // 物流信息
    async handleEditHostMan() {
      let { data } = await getPostMan({ id: 1106975712662 });
      this.postManList = data;

      this.dialogFormVisiblePostMan = true;

      // 处理数据
      for (var i = 0; i < data.length; i++) {
        let obj = { type: "primary", icon: "el-icon-more", color: "#f0f" };

        data[i] = { ...data[i], ...obj };
      }
      // console.log(data);
      this.postManList = data;
    },
  },
  filters: {
    filterDate(val, data1) {
      // console.log(val)
      let date = new Date(val)
      let y = date.getFullYear();
      let m = date.getMonth() + 1;
      let d = date.getDate();
      let h = date.getHours();
      let mm = date.getMinutes();
      let s = date.getSeconds();

      m = m < 10 ? "0" + m : m;
      d = d < 10 ? "0" + d : d;
      h = h < 10 ? "0" + h : h;
      mm = mm < 10 ? "0" + mm : mm;
      s = s < 10 ? "0" + s : s;

      if (data1 && data1 == "yyyy-mm-dd") {
        return `${y}-${m}-${d}`;
      }
      return `${y}-${m}-${d} ${h}:${mm}:${s}`;
    },
  },
};
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>